package com.msstudio;

// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 

import com.msstudio.midlets.MSCMIDlet;
import javax.microedition.lcdui.*;

public class FriendList extends Canvas
    implements CommandListener
{//�����б��ࣻ

    private final int C_LineHigh = 18;
    public static final int C_Bmp_W = 14;
    private final int C_y0 = 0;
    private final int C_x0 = 6;
    private final int C_dMove = 2;
    private final int C_NullRoom = 2;
    private final int C_Status_High = 16;
    public static final int C_DrawBmpNum = 10;
    private int iDrawBmpNum;
    private int iScreenWidth;
    private int iScreenHigh;
    protected Command cmdMenu;
    protected Command cmdExit;
    protected Command cmdBack;
    protected Command cmdOKtoExit;
    private int iTimeCnt;
    public int iShowAll;
    public int iGroup;
    public int iSelfState;
    private int iStatusBarMode;
    private String strInfor;
    private int inforCnt;
    private boolean iFirstTimeToShow;
    int iaIndexBuf[];
    int iaDrawIndex[];
    private int iDrawNum;
    private int iCur;
    private int iEnd;
    private int iSelect;
    private long iCurUIN;
    Icons theIcons;
    private boolean timerOn;

    public FriendList()
    {
        iDrawBmpNum = 4;
        iScreenWidth = 160;
        iScreenHigh = 120;
        cmdMenu = null;
        cmdExit = null;
        cmdBack = null;
        cmdOKtoExit = null;
        iTimeCnt = 0;
        iShowAll = 1;
        iGroup = 1;
        iSelfState = 10;
        iStatusBarMode = 1;
        strInfor = null;
        inforCnt = 0;
        iFirstTimeToShow = true;
        iaIndexBuf = new int[300];
        iaDrawIndex = new int[10];
        iDrawNum = 0;
        iCur = 0;
        iEnd = 0;
        iSelect = 0;
        iCurUIN = 0L;
        theIcons = null;
        timerOn = false;
        iScreenWidth = getWidth();
        iScreenHigh = getHeight();
        iDrawBmpNum = (iScreenHigh - 16 - 0) / 18;
    }

    public void init()
    {
        cmdMenu = new Command(MSCMIDlet.multiLang.sMenu, 1, 1);
        cmdExit = new Command(MSCMIDlet.multiLang.sExit, 2, 2);
        cmdBack = new Command(MSCMIDlet.multiLang.sBack, 2, 2);
        cmdOKtoExit = new Command(MSCMIDlet.multiLang.sYes, 1, 1);
        addCommand(cmdExit);
        addCommand(cmdMenu);
        setCommandListener(this);
        theIcons = new Icons();
        theIcons.init();
    }

    public void changeSelfState(int i)
    {
        iSelfState = i;
    }

    public void showInfor(String s)
    {
        strInfor = s;
        inforCnt = 6;
    }

    public void changeStatusBarMode()
    {
        switch(iStatusBarMode)
        {
        case 0: // '\0'
            iStatusBarMode = 1;
            iDrawBmpNum--;
            SetMainDrawData();
            break;

        case 1: // '\001'
            iStatusBarMode = 2;
            break;

        default:
            iStatusBarMode = 1;
            break;
        }
        repaint();
    }

    public void paint(Graphics g)
    {
        Object obj = null;
        int ai[][] = {
            {
                0, 0
            }, {
                -2, 2
            }, {
                0, 0
            }, {
                2, 2
            }
        };
        int l2 = iTimeCnt % 4;
        g.setFont(Font.getFont(0, 1, 0));
        int l1 = iScreenWidth;
        int i2 = 18;
        if(inforCnt > 0)
        {
            int i = 0;
            int l = iScreenHigh - 16;
            g.setColor(255);
            g.fillRect(i, l, l1, 16);
            g.setColor(65280);
            g.drawString(strInfor, 25, l + 1, 20);
        } else
        if(iStatusBarMode > 0)
        {
            int j = 0;
            int i1 = iScreenHigh - 16;
            g.setColor(255);
            g.fillRect(j, i1, l1, 16);
            g.drawImage(theIcons.getSysImage(iSelfState / 10 - 1), iScreenWidth - 16, i1, 20);
            if(l2 > 1)
                if(MSCMIDlet.getMsgDlg.getSysMsgNum() > 0)
                {
                    g.drawImage(theIcons.getSysImage(1), 2, i1, 20);
                } else
                {
                    long l3 = MSCMIDlet.getMsgDlg.getNextNeedRead();
                    if(l3 > 0L)
                    {
                        int j2 = MSCMIDlet.friendRecord.getUserLocation(l3);
                        short word0 = MSCMIDlet.friendRecord.getFaceIDbyIndex(j2);
                        g.drawImage(theIcons.getHeadImage(word0, 10), 2, i1, 20);
                    }
                }
            g.setColor(0);
            String s;
            if(iStatusBarMode == 1)
            {
                if(iGroup == 1)
                {
                    if(iShowAll != 0)
                        s = MSCMIDlet.multiLang.sAllFriend;
                    else
                        s = MSCMIDlet.multiLang.sOnlineFriend;
                } else
                {
                    s = MSCMIDlet.multiLang.sStranger;
                }
            } else
            {
                s = MSCMIDlet.httpPoster.GetSelfUIN();
            }
            g.drawString(s, iScreenWidth / 2, i1 + 1, 17);
        }
        g.setColor(255);
        if(iDrawNum == 0)
            g.fillRect(0, 0, iScreenWidth, i2);
        for(int k2 = 0; k2 < iDrawNum; k2++)
        {
            int k = 6;
            int j1 = 0 + k2 * 18;
            short word2 = MSCMIDlet.friendRecord.getStatebyIndex(iaDrawIndex[k2]);
            short word1 = MSCMIDlet.friendRecord.getFaceIDbyIndex(iaDrawIndex[k2]);
            if(iCur == k2)
                g.setColor(255);
            else
                g.setColor(0xffffff);
            g.fillRect(0, j1, iScreenWidth, i2);
            j1 += 2;
            long l4 = MSCMIDlet.friendRecord.getUserIDbyIndex(iaDrawIndex[k2]);
            if(MSCMIDlet.getMsgDlg.findMsgByUIN(l4) >= 0)
            {
                k += ai[l2][0];
                j1 += ai[l2][1];
            }
            g.drawImage(theIcons.getHeadImage(word1, word2), k, j1, 20);
            g.setColor(0);
            String s1 = MSCMIDlet.friendRecord.getNamebyIndex(iaDrawIndex[k2]);
            if(s1.length() == 0)
                s1 = "" + l4;
            g.drawString(s1, k + 25, j1 - 1, 20);
        }

        int k1 = 0 + iDrawNum * 18;
        if(iDrawNum == 0)
            k1 += 18;
        i2 = iScreenHigh - 16 - k1;
        if(iStatusBarMode == 0)
            i2 += 16;
        if(i2 > 0)
        {
            g.setColor(0xffffff);
            g.fillRect(0, k1, iScreenWidth, i2);
        }
    }

    public void keyPressed(int i)
    {
label0:
        switch(i)
        {
        case 49: // '1'
            OnPre();
            break;

        case 56: // '8'
            break;

        case 50: // '2'
            OnActive();
            break;

        case 51: // '3'
            OnFirst();
            break;

        case 52: // '4'
            OnNext();
            break;

        case 53: // '5'
            OnSysMsg();
            break;

        case 54: // '6'
            OnLast();
            break;

        case 42: // '*'
            if(iShowAll == 0)
                OnShowAll();
            else
                OnShowOnline();
            break;

        case 35: // '#'
            changeStatusBarMode();
            break;

        case 36: // '$'
        case 37: // '%'
        case 38: // '&'
        case 39: // '\''
        case 40: // '('
        case 41: // ')'
        case 43: // '+'
        case 44: // ','
        case 45: // '-'
        case 46: // '.'
        case 47: // '/'
        case 48: // '0'
        case 55: // '7'
        default:
            int k = getGameAction(i);
            int j;
            switch(k)
            {
            case 3: // '\003'
            case 4: // '\004'
            case 7: // '\007'
            default:
                break label0;

            case 1: // '\001'
                if(CheckMoveUp() == 0)
                    break label0;
                if(iCur > 0)
                    iCur--;
                SetMainDrawData();
                break label0;

            case 6: // '\006'
                if(CheckMoveDown() == 0)
                    break label0;
                if(iCur < iDrawBmpNum - 1)
                    iCur++;
                SetMainDrawData();
                break label0;

            case 2: // '\002'
                OnFriend();
                break label0;

            case 5: // '\005'
                OnStranger();
                break label0;

            case 8: // '\b'
                j = GetSelect();
                break;
            }
            if(j == -1)
                break;
            long l = MSCMIDlet.friendRecord.getUserIDbyIndex(j);
            if(MSCMIDlet.getMsgDlg.findMsgByUIN(l) >= 0)
                MSCMIDlet.getMsgDlg.show(true, l);
            else
                MSCMIDlet.sendMsgDlg.show(l, false);
            break;
        }
    }

    public void startTimer()
    {
        timerOn = true;
    }

    public void stopTimer()
    {
        timerOn = false;
    }

    public void OnTimer()
    {
        if(!timerOn)
            return;
        iTimeCnt++;
        if(inforCnt > 0)
            inforCnt--;
        repaint();
    }

    public boolean existFriend(String s)
    {
        int i = MSCMIDlet.friendRecord.getUserLocation(s);
        return i >= 0 && i < 300;
    }

    public void buildFriendList()
    {
        if(iFirstTimeToShow && MSCMIDlet.friendRecord.getFriendNum() == 0)
        {
            MSCMIDlet.instance.showMessage(MSCMIDlet.multiLang.sMSC, MSCMIDlet.multiLang.sUHaveNoFriend, this, cmdBack, null);
            return;
        } else
        {
            iEnd = MSCMIDlet.friendRecord.GetActiveData(iaIndexBuf, iGroup, iShowAll);
            int i = MSCMIDlet.friendRecord.getUserLocation(iCurUIN);
            SetSelectByIndex(i);
            SetMainDrawData();
            iFirstTimeToShow = false;
            return;
        }
    }

    public void commandAction(Command command, Displayable displayable)
    {
        if(command == cmdMenu)
        {
            int i = GetSelect();
            if(i >= 0)
                MSCMIDlet.mainMenu.show(MSCMIDlet.friendRecord.getUserIDbyIndex(i));
            else
                MSCMIDlet.mainMenu.show(0L);
            return;
        }
        if(command == cmdExit)
        {
            MSCMIDlet.instance.showMessage(MSCMIDlet.multiLang.sMSC, MSCMIDlet.multiLang.sYesToExit, this, cmdOKtoExit, cmdBack);
            return;
        }
        if(command == cmdBack)
        {
            MSCMIDlet.display.setCurrent(this);
            return;
        }
        try
        {
            if(command == cmdOKtoExit)
            {
                MSCMIDlet.friendRecord.stopTimer();
                MSCMIDlet.getMsgDlg.stopTimer();
                MSCMIDlet.mainForm.stopTimer();
                MSCMIDlet.instance.showMessage(MSCMIDlet.multiLang.sMSC, MSCMIDlet.multiLang.sLogouting, this, null, null);
                MSCMIDlet.friendRecord.saveFriendInfor();
                MSCMIDlet.httpPoster.logout(MSCMIDlet.mainMenu);
                return;
            }
        }
        catch(Exception exception) { }
        return;
    }

    private void SetMainDrawData()
    {
        int i1 = 0;
        if(iEnd == 0)
        {
            iDrawNum = 0;
            for(int i = 0; i < 10; i++)
                iaDrawIndex[i] = -1;

            iCurUIN = 0L;
            repaint();
            return;
        }
        if(iSelect >= iEnd)
            iSelect = iEnd - 1;
        int j1 = iEnd;
        if(iEnd - i1 < iDrawBmpNum)
        {
            iCur = iSelect;
        } else
        {
            i1 = iSelect - iCur;
            if(i1 < 0)
            {
                i1 = 0;
                iCur = iSelect - i1;
            }
            if(i1 + iDrawBmpNum <= iEnd)
            {
                j1 = i1 + iDrawBmpNum;
            } else
            {
                int j = (i1 + iDrawBmpNum) - iEnd;
                iCur += j;
                i1 = iEnd - iDrawBmpNum;
                j1 = iEnd;
            }
        }
        iDrawNum = j1 - i1;
        for(int k = i1; k < j1; k++)
            iaDrawIndex[k - i1] = iaIndexBuf[k];

        for(int l = j1; l < 10; l++)
            iaDrawIndex[l] = -1;

        if(!iFirstTimeToShow)
        {
            int k1 = GetSelect();
            if(k1 >= 0)
                iCurUIN = MSCMIDlet.friendRecord.getUserIDbyIndex(k1);
            else
                iCurUIN = 0L;
        }
        repaint();
    }

    private int CheckMoveUp()
    {
        if(iSelect > 0)
        {
            iSelect--;
            return 1;
        } else
        {
            return 0;
        }
    }

    private int CheckMoveDown()
    {
        if(iSelect + 1 < iEnd)
        {
            iSelect++;
            return 1;
        } else
        {
            return 0;
        }
    }

    public int IfCanJumpUp()
    {
        return iSelect - iCur <= 0 ? 0 : 1;
    }

    public int IfCanJumpDown()
    {
        return iSelect - iCur < iEnd - iDrawBmpNum ? 1 : 0;
    }

    public int GetSelect()
    {
        if(iSelect >= iEnd)
            return -1;
        else
            return iaIndexBuf[iSelect];
    }

    public void OnFriend()
    {
        if(iGroup != 1)
        {
            iGroup = 1;
            iSelect = 0;
            iCur = 0;
            buildFriendList();
        }
    }

    public void OnStranger()
    {
        if(iGroup != 2)
        {
            iGroup = 2;
            iSelect = 0;
            iCur = 0;
            buildFriendList();
        }
    }

    public void OnFirst()
    {
        int i = GetSelect();
        if(i == -1)
        {
            return;
        } else
        {
            iSelect = 0;
            iCur = 0;
            SetMainDrawData();
            return;
        }
    }

    public void OnLast()
    {
        int i = GetSelect();
        if(i == -1)
        {
            return;
        } else
        {
            iSelect = iEnd - 1;
            iCur = iDrawBmpNum - 1;
            SetMainDrawData();
            return;
        }
    }

    public void OnActive()
    {
        long l = MSCMIDlet.getMsgDlg.getNextNeedRead();
        if(l == 0L)
            return;
        int i = MSCMIDlet.friendRecord.getUserLocation(l);
        switch(MSCMIDlet.friendRecord.getGroupByIndex(i))
        {
        case 1: // '\001'
            OnFriend();
            if(MSCMIDlet.friendRecord.OnlineAndAway(i) == 0)
                OnShowAll();
            break;

        case 2: // '\002'
            OnStranger();
            break;

        default:
            return;
        }
        SetSelectByIndex(i);
        SetMainDrawData();
    }

    private int SetSelectByIndex(int i)
    {
        for(int j = 0; j < iEnd; j++)
            if(iaIndexBuf[j] == i)
            {
                iSelect = j;
                return 1;
            }

        return 0;
    }

    public void OnPre()
    {
        if(IfCanJumpUp() == 0)
            return;
        iSelect -= iDrawBmpNum;
        if(iSelect < 0)
            iSelect = 0;
        if(iSelect < iCur)
        {
            iCur = 0;
            iSelect = 0;
        }
        SetMainDrawData();
    }

    public void OnNext()
    {
        if(IfCanJumpDown() == 0)
            return;
        iSelect += iDrawBmpNum;
        if(iSelect >= iEnd)
            iSelect = iEnd - 1;
        if(iEnd - iSelect < iDrawBmpNum - iCur)
        {
            iCur = iDrawBmpNum - 1;
            iSelect = iEnd - 1;
        }
        SetMainDrawData();
    }

    public void OnShowAll()
    {
        iShowAll = 1;
        buildFriendList();
    }

    public void OnShowOnline()
    {
        iShowAll = 0;
        buildFriendList();
    }

    public void OnSysMsg()
    {
        MSCMIDlet.getMsgDlg.show(false, 0L);
    }
}
